import {Cell, Graph, PopupMenuHandler, PopupMenuItem} from '@maxgraph/core';
import "@/styles/graphMenu.css";
import {copy, cut, paste} from "@/publicFuns/graphAction.ts";
function createPopupMenu(graph: Graph, menu, cell: Cell | null, evt: MouseEvent)
{
    menu.addItem('剪切', null, ()=>{
        cut(graph)
    })
    menu.addItem('复制', null, ()=>{
        copy(graph)
    })
    menu.addItem('粘贴', null, ()=>{
        paste(graph)
    })
    menu.addItem('删除', null, ()=>{
        if (!cell) {
            return
        }
        graph.removeCells([cell])
    }, menu, null, ()=>{return cell !== null}, true, false)
    // console.log(menu, typeof menu)
    menu.addItem('ZoomOut', null, function() {
        graph.zoomOut()
    }, null, null, true, true, false);

    menu.addItem('ZoomIn', null, function():void
    {
        graph.zoomIn();
    }, null, null, true, true, false);
    menu.addSeparator();
    // 创建二级菜单 ，注意父菜单的参数位置
    const subMenus1: PopupMenuItem = menu.addItem('hello', null,null, null, null, true, true, false)
    menu.addItem('hello', null, ()=>{
        console.log('hello')
    }, subMenus1, null, true, true, false)
    menu.addItem('world', null, ()=>{
        console.log('world')
    }, subMenus1, null, true, true, false)
}
export default function (graph: Graph) {
    const popupMenu: PopupMenuHandler = graph.getPlugin('PopupMenuHandler') as PopupMenuHandler
    popupMenu.autoExpand = true 	// 鼠标放到二级菜单时自动子项自动展开
    popupMenu.factoryMethod = function(menu: PopupMenuItem, cell: Cell | null, evt:MouseEvent)
    {
        if (cell) {
            graph.scrollCellToVisible(cell)
        }
        // 创建菜单
        return createPopupMenu(graph, menu, cell, evt);
    };
}